package com.sinoma.G2project.MoYu.Service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.firenay.common.vo.MemberRsepVo;
import com.sinoma.G2project.MoYu.Dao.MoYu.MoYuDao;
import com.sinoma.G2project.MoYu.Service.DataSelectService;
import com.sinoma.G2project.MoYu.entity.MoYuEntity;
import com.sinoma.G2project.MoYu.vo.MoYuVo;
import com.sinoma.G2project.config.datasource.DataSwitch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

@Slf4j
@DataSwitch("Moyu")
@Service("dataSelectService") // 接口 DataSelectService 这个首字母小写
public class DataSelectServiceImpl extends ServiceImpl<MoYuDao, MoYuEntity> implements DataSelectService {

    @Override
    @Cacheable(value = {"moyu"}, key = "#root.method.name", sync = true)
    public MoYuVo getLatest() {
        MoYuEntity latest = this.baseMapper.SelectlastestRelation().get(0);
        MoYuVo moYuVo = new MoYuVo();
        BeanUtils.copyProperties(latest,moYuVo);
        return moYuVo;
    }

    @Override
    public MoYuVo checkUser(MoYuVo vo, Object attribute) {
        if (attribute instanceof MemberRsepVo){
            MemberRsepVo attribute1 = (MemberRsepVo) attribute;
            Class<?> clazz = vo.getClass();
            Field[] fields = clazz.getDeclaredFields();
            Boolean SC_ON = false;
            Boolean b = false;
            for (Field field : Arrays.asList(fields)) {
                if(field.getName().contains("SC_YW_FK_WLL")){
                    b = true;
                    break;
                };
            }
            if (b){
                try {
                    PropertyDescriptor pd = new PropertyDescriptor("SC_YW_FK_WLL", clazz);
                    Method method = pd.getReadMethod();
                    Float val = (Float) method.invoke(vo);
                    if (val >= 200){
                        SC_ON = true;
                    }
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IntrospectionException e) {
                    e.printStackTrace();
                }
            }
            // 展示 且 开窑
            Object[] val = {1f};
            if("show".equals(attribute1.getUsername()) && SC_ON)  {
                for (Field field : fields) {
                    if (((field.getName().contains("_MA_") &&
                            field.getName().contains("YL_") &&
                            (!(field.getName().contains("YZS"))) &&
                            (!(field.getName().contains("_TM"))))) ||
                            ((field.getName().contains("_MA_") &&
                            field.getName().contains("SC_") &&
                            (!(field.getName().contains("YZS"))) &&
                            (!(field.getName().contains("_TM")))))) {
                        try {
                            PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                            Method method = pd.getWriteMethod();
                            method.invoke(vo,1);
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (IntrospectionException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        return vo;
    }

}
